﻿using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Info;

namespace Mandarine.Util
{
    /// <summary>
    /// 各種ユーティリティを提供します
    /// </summary>
    public static class Util
    {
        private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        public static bool DoHandle { get; set; }
        
        /// <summary>
        /// スタティックコンストラクタ
        /// </summary>
        static Util()
        {
            DoHandle = true;

            Application.Current.UnhandledException +=
                (s, e) =>
                {
                    if (DoHandle)
                    {
                        //Handling the exception within the UnhandledException handler.
                        MessageBox.Show(e.ExceptionObject.StackTrace
                            , e.ExceptionObject.Message
                            , MessageBoxButton.OK);

                        _logger.ErrorException(e.ExceptionObject.Message, e.ExceptionObject);
                        e.Handled = true;
                    }
                    else
                    {
                        //If you do not set e.Handled to true, the application will close due to crash.
                        MessageBox.Show(e.ExceptionObject.StackTrace
                            , "想定外の例外が発生" + "\r\n" + "アプリケーションを終了します"
                            , MessageBoxButton.OK);
                        _logger.ErrorException(e.ExceptionObject.Message, e.ExceptionObject);
                        e.Handled = false;
                    }
                };
            _logger.Debug("Util class loaded DoHandle = " + DoHandle.ToString());

        }

        private const int ANIDLength = 32;
        private const int ANIDOffset = 2;

        /// <summary>
        /// このクラスをロードするための初期化ダミーメソッド
        /// </summary>
        public static void Init()
        {
            //dummy
        }

        /// <summary>
        /// 現在のアクティブページを取得します
        /// </summary>
        /// <returns></returns>
        public static PhoneApplicationPage GetActivePage()
        {
            PhoneApplicationPage result = null;
            Application current = Application.Current;
            if (current != null)
            {
                var phoneApplicationFrame = current.RootVisual as PhoneApplicationFrame;
                if (phoneApplicationFrame != null)
                {
                    result = (phoneApplicationFrame.Content as PhoneApplicationPage);
                }
            }
            return result;
        }
        /// <summary>
        /// ハードウェァ製造元の名前を取得します
        /// </summary>
        /// <returns></returns>
        public static string GetManufacturer()
        {
            string result = string.Empty;
            object manufacturer;
            if (DeviceExtendedProperties.TryGetValue("DeviceManufacturer", out manufacturer))
                result = manufacturer.ToString();

            return result;
        }
        /// <summary>
        /// デバイス固有のID値を取得します
        /// WMAppManifest.xmlに "ID_CAP_IDENTITY_DEVICE"の許可が必要です
        /// </summary>
        /// <returns></returns>
        public static byte[] GetDeviceUniqueID()
        {
            byte[] result = null;
            object uniqueId;
            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
                result = (byte[])uniqueId;

            return result;
        }
        /// <summary>
        /// WindowsLiveのアノニマスIDを取得します
        /// WMAppManifest.xmlに "ID_CAP_IDENTITY_USER"の許可が必要です
        /// </summary>
        /// <returns></returns>
        public static string GetWindowsLiveAnonymousID()
        {
            string result = string.Empty;
            object anid;
            if (UserExtendedProperties.TryGetValue("ANID", out anid))
            {
                if (anid != null && anid.ToString().Length >= (ANIDLength + ANIDOffset))
                {
                    result = anid.ToString().Substring(ANIDOffset, ANIDLength);
                }
            }

            return result;
        }
    }
}